package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.UserModel;
import com.hangzhou.xyj.net.handler.QuitGroupHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.QuitGroupReq;
import com.hangzhou.xyj.ui.activity.HomePageActivity;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.LogUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaohuix on 2016/5/9  16:57
 * <p/>
 * 说明：群成员 适配器
 */
public class GroupMemberHeadersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private static final String TAG = "GroupMemberHeadersAdapter";
    private Context mContext;
    private List<UserModel> mDataList;
    private LayoutInflater mInflater;
    private String mGroupId;
    private String mGroupMasterId = "0";//群主ID，默认为0

    public GroupMemberHeadersAdapter(Context context, List<UserModel> datas) {
        this.mContext = context;
        this.mDataList = datas;
        this.mInflater = LayoutInflater.from(context);
    }

    /**
     * 设置群id：19816515615616等
     *
     * @param groupId
     */
    public void setGroupId(String groupId) {
        this.mGroupId = groupId;
    }

    /**
     * 设置群ID
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/5/10 10:41
     */
    public void setGroupMasterId(String id) {
        this.mGroupMasterId = id;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return 0;
            default:
                return 1;
        }
    }

    @Override
    public int getItemCount() {
        if (mDataList == null || mDataList.isEmpty()) {
            mDataList = new ArrayList<>();
        }
        return mDataList.size();
    }

    @Override
    public long getHeaderId(int position) {
        switch (position) {
            case 0:
                return 0;
            default:
                return 1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return new HeaderItemViewHolder(mInflater.inflate(R.layout.layout_group_item_header, parent, false));
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        HeaderItemViewHolder headerItemViewHolder;
        if (holder == null) {
            return;
        } else {
            headerItemViewHolder = (HeaderItemViewHolder) holder;
        }
        switch ((int) getHeaderId(position)) {
            case 0:
                headerItemViewHolder.mTvGroupName.setText("群主");
                break;
            default:
                headerItemViewHolder.mTvGroupName.setText("群成员");
                break;
        }
    }

    /**
     * 群主是不是自己
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/5/10 10:39
     */
    private boolean isMyGroup() {
        return mGroupMasterId.equals(XYJApplication.getUserInfo().getUserId());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (isMyGroup() && viewType == 1) {//只有自己为群主 才有移除群成员功能 且 不是群主
            return new GroupMemberViewHolder(mInflater.inflate(R.layout.layout_member_item, parent, false));
        }
        return new GroupMemberViewHolder(mInflater.inflate(R.layout.layout_member_with_nocancel_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final UserModel userModel = mDataList.get(position);
        ((GroupMemberViewHolder) holder).setData(userModel);
    }


    public void update(List<UserModel> list) {
        if (list == null || list.isEmpty()) {
            list = new ArrayList<>();
        }
        mDataList = list;
        notifyDataSetChanged();
    }

    /**
     * Header部分的ViewHolder
     */
    private class HeaderItemViewHolder extends RecyclerView.ViewHolder {
        MyTextView mTvMore;
        MyTextView mTvGroupName;

        public HeaderItemViewHolder(View itemView) {
            super(itemView);
            mTvGroupName = (MyTextView) itemView.findViewById(R.id.tv_header_group);
            mTvMore = (MyTextView) itemView.findViewById(R.id.tv_more);
            //隐藏”更多“
            mTvMore.setVisibility(View.GONE);
        }
    }

    public class GroupMemberViewHolder extends RecyclerView.ViewHolder {
        CircleImageView mImgView;
        MyTextView mTvName;
        MyTextView mTvDelete;//删除按钮
        private UserModel mUserModel;

        public GroupMemberViewHolder(View itemView) {
            super(itemView);
            mImgView = (CircleImageView) itemView.findViewById(R.id.civ_header);
            mTvName = (MyTextView) itemView.findViewById(R.id.mtv_name);
            mTvDelete = (MyTextView) itemView.findViewById(R.id.mtv_delete);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, HomePageActivity.class);
                    intent.putExtra(DataKey.INDEX, mUserModel.getUser_id());
                    intent.putExtra(DataKey.TITLE, mUserModel.getNickName());
                    IntentManager.getInstance().goActivity(mContext, intent);
                }
            });

            if (mTvDelete != null) {
                mTvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeMember(mUserModel);
                    }
                });
            }
        }

        public void setData(final UserModel userModel) {
            this.mUserModel = userModel;

            mImgView.displayImage(userModel.getHeadPic());
            mTvName.setText(userModel.getNickName());


        }
    }

    /**
     * 移除成员
     *
     * @param userModel
     */
    private void removeMember(final UserModel userModel) {
        LogUtil.d("debug", "groupId==" + mGroupId + "   userId==" + userModel.getUser_id());
        QuitGroupReq req = new QuitGroupReq(userModel.getUser_id(), mGroupId);
        HttpLoader.getInstance().sendRequest(new QuitGroupHandler(req, new HttpResponseListener() {
            @Override
            public void onStart() {
                LogUtil.d("debug","quit group onStart");
            }

            @Override
            public void onSuccess(Object model) {
                LogUtil.d("debug","quit group onSuccess");
                //踢人成功
                //移除了用户
                mDataList.remove(userModel);
                notifyItemRemoved(mDataList.indexOf(userModel));
            }

            @Override
            public void onFail(String msg) {
                LogUtil.d("debug","quit group onFail");
                ToastUtil.show("移除成员失败");
            }

            @Override
            public void onDone(String msg) {
                LogUtil.d("debug","quit group onDone");
            }

            @Override
            public void onError() {
                LogUtil.d("debug","quit group onError");
            }
        }, 0));
    }

}

